const days = require("./libs/dayjs.min");
const duration = require("./libs/dayjs-plugin/duration.min");
const relativeTime = require("./libs/dayjs-plugin/relativeTime.min");
days.locale('zh-cn');
days.extend(duration);
days.extend(relativeTime);

function isBetweenTime(startTime, endTime, nowTime) {
    let time = days();
    if (nowTime) {
        time = days.unix(nowTime);
    }
    return !!(time.isBefore(days.unix(endTime)) && time.isAfter(days.unix(startTime)));
}

function isBeforeTime(startTime, nowTime) {
    let time = days();
    if (nowTime) {
        time = days.unix(nowTime);
    }

    return !!(time.isBefore(days.unix(startTime)));
}

function isAfterTime(endTime, nowTime) {
    let time = days();
    if (nowTime) {
        time = days.unix(nowTime);
    }
    return !!(time.isAfter(days.unix(endTime)));
}

function formatTime(time, format = "YYYY-MM-DD HH:mm:ss") {
    return days.unix(time).format(format);
}

function timeToNow(time) {
    const diff = days.duration(days.unix(time).diff(days())).$d;
    let str = "";
    if (diff.years) {
        str += `${diff.years}年`;
    }
    if (diff.months) {
        str += `${diff.months}月`;
    }
    if (diff.days) {
        str += `${diff.days}天`;
    }
    str += `${(diff.hours + '').padStart(2, 0)}小时`;
    str += `${(diff.minutes + '').padStart(2, 0)}分钟`;
    str += `${(diff.seconds + '').padStart(2, 0)}秒`;
    return str;
}

function timeToNowS(time) {
    const diff = days.duration(days.unix(time).diff(days())).asSeconds();
    return Math.floor(diff);
}

module.exports = {
    days: days(),
    isBetweenTime,
    isBeforeTime,
    isAfterTime,
    formatTime,
    timeToNow,
    timeToNowS
}